<?php

namespace app\admin\command;

use app\admin\model\cms\GrapevineModel;
use app\admin\model\cms\Live;
use app\api\service\FetchArticleService;
use app\api\service\TengxunSignatureUtil;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;

/**
 * 定时任务-api获取腾讯会议直播回放地址(第三方直播)
 * Class GetLivePlayReplays
 * @package app\admin\command
 */
class GetLivePlayReplays extends Command
{
    protected function configure()
    {
        $this->setName('getLivePlayReplays')->setDescription('api获取腾讯会议直播回放地址');
    }

    protected function execute(Input $input, Output $output)
    {
        // 执行具体的任务逻辑
//        $data = GrapevineModel::where('status', 0)
//            ->where('failure_count', '<', 3)
//            ->select();
//
//        $service = new FetchArticleService();//api根据链接抓取文章
//        foreach ($data as $v) {
//            $getArticle = $this->getArticle($service, $v['form_url']);
//            if ($getArticle['code'] == 200) {
//                $update_data = [
//                    'content' => $getArticle['html_content'],
//                    'status' => 1,
//                    'failure_count' => 0,
//                ];
//            } else {
//                $update_data = [
//                    'content' => $getArticle['html_content'],
//                    'status' => 2,
//                    'remark' => $getArticle['msg'],
//                    'failure_count' => 3,
//                ];
//            }
//            GrapevineModel::where('id', $v['id'])->update($update_data);
//        }

        // 同步腾讯会议直播回放地址
        $this->getLiveReplays();
    }

    private function getArticle($obj, $url, $num = 1)
    {
        $res = [
            'code' => 200,
            'msg' => 'success',
            'title' => '',
            'html_content' => '',
        ];
        $fetchArticle = $obj->fetchArticle($url);
        if ($fetchArticle['code'] == 200) {
            $res['title'] = $fetchArticle['title'];
            $res['html_content'] = $fetchArticle['html_content'];
        } else {
            $res['code'] = 500;
            $res['msg'] = $fetchArticle['msg'];
            if ($num < 4) {
                $this->getArticle($obj, $url, ($num + 1));
            }
        }

        return $res;
    }

    /**
     * 获取会议录屏内容
     * @param $meeting_info | 直播会议的meet_info字段数据
     * @return mixed|string|null
     */
    public function getLiveReplays()
    {
        //获取最近2天开始直播且存在腾讯会议的配置的信息的数据且没有直播回放地址的数据
        $data = Live::whereNotNull('meet_info')//腾讯会议创建信息
        ->where('is_start_live', 1)//开始直播的
        ->where('live_play_replays_url', null)//直播回放地址空的
        ->select();
        $live_play_replays_url_arr = [];
        //调用腾讯会议api获取直播回放地址,更新到数据库
        $tengxunmeet = new TengxunSignatureUtil();//腾讯会议服务类
        foreach ($data as $v) {
            $meet_info = json_decode($v['meet_info'], true, 512, JSON_UNESCAPED_UNICODE);
            $r = $tengxunmeet->getLiveReplays($meet_info);
            if (!empty($r)) {
                $live_play_replays_url_arr[] = [
                    'id' => $v['id'],
                    'live_play_replays_url' => json_encode($r),
                ];
            }
        }
        foreach ($live_play_replays_url_arr as $item) {
            Live::where('id', $item['id'])->update(['live_play_replays_url' => $item['live_play_replays_url']]);
        }
    }
}